<template>




  <div style="padding: 10px">

    <div style="height: 50px;line-height: 50px;border-bottom: 1px solid #ccc;display: flex">
      <div style="width: 200px;padding-left: 30px;font-weight: bold;color: black;font-size: x-large">NCMS系统</div>
      <el-link @click.native="$router.push('/Main')" style="margin-left: 550px">首页</el-link>
      <el-link @click.native="$router.push('/Activity')" style="margin-left: 100px">社团活动</el-link>
      <div style="flex: 1"></div>
      <div style="width: 100px">
        <el-dropdown>
        <span class="el-dropdown-link">
        我的<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item style="background-color: white;color: #333333" @click.native="$router.push('/Uinformation')">个人信息</el-dropdown-item>
            <el-dropdown-item style="background-color: white;color: #333333"
                              @click.native="$router.push('/Login')">退出系统</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>

    <div style="height: 20px">
    </div>


    <div style="background-image: url('../assets/images/背景.jpg');background-size: 100% 100%">


    <div style="margin-left: 840px;">
      <h3 style="margin: 0 auto;padding: 10px">最新活动</h3>
    </div>


    <el-table
        :data="tableData"
        stripe
        style="width: 50%;margin: 20px auto">
      <el-table-column
          prop="title"
          label="标题">
      </el-table-column>
      <el-table-column
          prop="author"
          label="作者">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="出版时间">
      </el-table-column>
      <el-table-column label="查看">
        <template #default="scope">
          <el-button @click="details(scope.row)" size="mini">详情</el-button>
        </template>
      </el-table-column>

    </el-table>

<!--    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>-->



      <el-dialog title="详情" :visible.sync="vis" style="min-height: 1000px" width="50%">
        <el-card>
          <div v-html="detail.content"></div>
        </el-card>
      </el-dialog>

    </div>



    <div style="width: 100%;height: 30px;background-color: darkgray;margin-top: 300px">
    </div>

    <div style="width: 100%;height: 100px;background-color: #333333">
      <h4 style="color: white;margin-left: 780px">联系我们|关于我们|合作电话</h4>
    </div>

  </div>


</template>

<script>

import E from 'wangeditor'
import request from "../utils/request";
import Header2 from "../components/Header2";
import Aside2 from "../components/Aside2";

let editor;

export default {
  name: 'Essay',
  components: {
    Aside2,
    Header2

  },
  data()
  {
    return{
      form:{},
      dialogVisible:false,
      search:'',
      currentPage4:1,
      pageSize:10,
      total:0,
      tableData: [],
      vis:false,
      detail:{}
    }
  },
  created() {
    this.load()
  },
  methods:{
    details(row){
      this.detail = row
      this.vis = true
    },
    load()
    {
      request.get("/essay",{
        params:{
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search:this.search
        }
      }).then(res=>{
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.records.length
      })
    },
    add()
    {
      this.dialogVisible = true
      this.form = {}

      this.$nextTick(()=>{
        //关联弹窗了吗的div。new 一个editor对象
        editor = new E('#div1')

        //配置server接口地址
        editor.config.uploadImgServer = 'http://localhost:9090/files/eidtor/upload'
        editor.config.uploadFileName = "file";
        editor.create()
      })
    },
    handleDelete(id)
    {
      console.log(id)
      request.delete("/essay/" + id).then(res=>{
        if(res.code === '0')
        {
          this.$message({
            type:"success",
            message:"删除成功"
          })
        } else {
          this.$message({
            type:"error",
            message:res.msg
          })
        }
        this.load()
      })
    },
    save()
    {
      this.form.content = editor.txt.html()//获取编辑器里面的值 ，放在实体中
      if(this.form.id) //更新
      {
        request.put("/essay",this.form).then(res=>{
          console.log(res)
          if(res.code === '0')
          {
            this.$message({
              type:"success",
              message:"更新成功"
            })
          } else {
            this.$message({
              type:"error",
              message:res.msg
            })
          }
          this.load()//刷新表格的数据
          this.dialogVisible = false
        })
      } else {//新增
        let userStr = sessionStorage.getItem("user") ||"{}"
        let user = JSON.parse(userStr)
        this.form.author = user.username
        request.post("/essay", this.form).then(res => {
          console.log(res)
          if(res.code === '0')
          {
            this.$message({
              type:"success",
              message:"新增成功"
            })
          } else {
            this.$message({
              type:"error",
              message:res.msg
            })
          }
          this.load()//刷新表格的数据
          this.dialogVisible = false
        })
      }
    },
    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true

      if (!editor) {
        this.$nextTick(() => {
          //关联弹窗了吗的div。new 一个editor对象
          editor = new E('#div1')
          editor.create()
          editor.txt.html(row.content)
        })
      }


    },
    handleSizeChange(pageSize)//改变当前每页的个数触发
    {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum){ //改变当前页码触发
      this.currentPage4 = pageNum
      this.load()
    }
  }

}
</script>
